var Autocomplete = function () {
    return Autocomplete.fn.init();
}

// Declare
Autocomplete.fn = Autocomplete.prototype = {

	// Autocom
    input:null,
    content:null, // show result auto
	update:null, // show result duoc chon khi click vao item
	tmplPath:null,
	tmplName:null,
	url:null, // duong dan lay result
	parent:null,

	// Init
    init:function() {},


    /**
	 * Setup auto
	 *
	 * @param: options la doi tuong co cac thuoc tinh sau:
	 * 		- input: -> input nhap
	 * 		- content: -> noi noi dung auto se hien ra
	 * 		- update: -> show result duoc chon khi click vao item
	 * 		- url: -> tduong dan ge result
	 * 		- tmplPath: -> ten fodel chua file jstemplate
	 * 		- tmplName: -> ten file jstemplate
	 */
    setup: function(options){
		if(options) {

			// Attributes
			this.input = options.input != 'undefined' ? options.input : null;
			this.content = options.content != 'undefined' ? options.content : null;
			this.update = options.update != 'undefined' ? options.update : null;
			this.url = options.url != 'undefined' ? options.url : null;
			this.tmplPath = options.tmplPath != 'undefined' ? options.tmplPath : null;
			this.tmplName = options.tmplName != 'undefined' ? options.tmplName : null;

			// Bind event typing
			this.typing();
		}
    },

	/**
	 * Typing
	 *
	 * @param:
	 */
	timeout: 300, // thoi gian go
	timer: null, // doi tuong setTimeout
	typing:function()
    {
    	var _this = this;
	    this.input.keyup(function(event) {

			clearTimeout( _this.timer );

	        _this.timer = setTimeout(function(){

				_this.search(_this.url);

	        }, _this.timeout );

	    });
    },

    /**
	 * Search result
	 *
	 * @param:
	 */
	search: function(url)
	{
		var _this = this;

		if(url && url.length > 0) {

			var q = _this.input.val();
			var datanone = _this.input.attr('data-none');

			// Kiem tra xem input co du lieu hay khong
	       	if( q.trim().length > 0 ) {

				// Get data
				var url = url + '?q=' +q + '&datanone=' + datanone;

				$.when(
					loader.getAjax(url, false),
					template.load(_this.tmplName, _this.tmplPath)
				).then(function(resp, jstemplate){

					_this.closeStart();

					if($.isArray(resp))  resp = resp[0];

					// Render html
					var html = template.render(jstemplate, resp);
					_this.content.html(html);

					// Bind event click li
					_this.content.find('li').click(function(event){
						event.preventDefault();
		               	event.stopPropagation();

		               	//_this.closeStop();
						_this.input.val('').attr('value' , '');

						var li = $(this);
						if( !li.hasClass('empty') ) {
							// add data-none
							datanone += li.attr('id') + ',';
							_this.input.attr('data-none', datanone);

							_this.selected(event);
						}
					});
				});
	       	}
       	}
	},


	/**
	 * Selected
	 *
	 * @param: event -> la su kien cua doi tuong duoc click
	 */
	selected:function(event) {
		this.closeStart();
	},

	/**
	 *	Remove selected
	 **/
	removeSelected: function(obj) {
		if(obj) {
			var id = obj.attr('pid');
			var datanone = this.input.attr('data-none');
			datanone = datanone.replace(id + ',', '');
			this.input.attr('data-none', datanone);
			obj.closest('tr').remove();

			this.setPrice();
			return true;
		}
		return false;
	},

	/**
	 *	close start
	 **/
	closeStart:function() {
		var _this = this;
		$('body').unbind().click(function(event){
			event.stopPropagation();
			clearTimeout( _this.timer );
			_this.content.html('');
			_this.input.val('').attr('value' , '');
		});
	},

	/**
	 *	close stop
	 **/
	closeStop:function() {
		$('body').unbind();
	},
}

var autocomplete = new Autocomplete();

